package com.gongpingjialoan.activity.car;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.api.Piont;
import com.gongpingjialoan.manage.PointRecordManage;

/**
 * Created by Administrator on 2016/9/20.
 */
public class DealerMapActivity extends BaseActivity implements AMap.InfoWindowAdapter {

    private double latitude  , longitude;

    private MapView mMapView;

    private AMap aMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isSendData = true;
        action = "detail_map";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vip_map_layout);
        setTitle("地址");
        mMapView = (MapView) findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState); // 此方法必须重写
        aMap = mMapView.getMap();
        aMap.setInfoWindowAdapter(this);// 设置自定义InfoWindow样式

        latitude = getIntent().getDoubleExtra("latitude" , 0);
        longitude = getIntent().getDoubleExtra("longitude" , 0);

        MarkerOptions markerOption = new MarkerOptions();
        markerOption.draggable(false);
        markerOption.anchor(0.5f, 0.5f);
        markerOption.title("当前车源位置");
        LatLng ll = new LatLng(latitude, longitude);
        markerOption.position(ll);
//        markerOption.icon(BitmapDescriptorFactory
//                .fromResource(R.drawable.map_location));
        Marker marker = aMap.addMarker(markerOption);
        aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(ll, 15));
        marker.showInfoWindow();
    }


    /**
     * 方法必须重写
     */
    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        PointRecordManage.getInstance().addPoint(mySelf, Piont.SourceMapBack);
        mMapView.onDestroy();
    }

    @Override
    public View getInfoWindow(Marker marker) {
        View infoWindow = getLayoutInflater().inflate(
                R.layout.custom_info, null);
        render(marker, infoWindow);
        return infoWindow;
    }

    /**
     * 自定义infowinfow窗口
     */
    public void render(Marker marker, View view) {
        String title = marker.getTitle();
        TextView titleUi = ((TextView) view.findViewById(R.id.title));
        titleUi.setText(title);
    }

    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }
}

